package com.company.cloud.work.modules.mgr.service.impl;

import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.company.cloud.common.bean.BaseClientResult;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.company.cloud.common.bean.LoginSimpleUserInfo;
import com.company.cloud.common.bean.LoginUserInfo;
import com.company.cloud.common.constant.BaseConstant.EnabledEnum;
import com.company.cloud.common.utils.CheckUtils;
import com.company.cloud.user.modules.api.response.UserSimple;
import com.company.cloud.user.modules.api.service.UserRemoteService;
import com.company.cloud.work.modules.base.entity.WorkReceiptInfo;
import com.company.cloud.work.modules.base.service.WorkReceiptInfoService;
import com.company.cloud.work.modules.cache.models.WorkReceiptModel;
import com.company.cloud.work.modules.cache.utils.CrmWorkChangeUtils;
import com.company.cloud.work.modules.mgr.service.WorkReceiptMgrService;

@Service
public class WorkReceiptMgrServiceImpl implements WorkReceiptMgrService{

	
	@Autowired
	private WorkReceiptInfoService workReceiptInfoService;
	@Autowired
	private UserRemoteService userRemoteService;

	
	@Override
	public void save(String idType, String id, String remark,LoginSimpleUserInfo userInfo) {
		WorkReceiptInfo entity=workReceiptInfoService.getOne(new QueryWrapper<WorkReceiptInfo>()
				.eq("company_no", userInfo.getCompanyNo())
				.eq("user_id", userInfo.getId())
				.eq("rel_biz_type", idType)
				.eq("rel_biz_id", id)
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				);
		
		if(entity==null) {
			entity=new WorkReceiptInfo();
			entity.setCompanyNo(userInfo.getCompanyNo());
			entity.setUserId(userInfo.getId());
			entity.setRelBizType(idType);
			entity.setRelBizId(id);
			entity.setReceiptDate(LocalDateTime.now());
			entity.setRemark(remark);
			entity.addCreateParam(userInfo);
			entity.addEditParam(userInfo);
			workReceiptInfoService.save(entity);
		}
		
	}


	@Override
	public List<WorkReceiptModel> listByRel(String idType, String id, LoginSimpleUserInfo userInfo) {
		List<WorkReceiptInfo> list= workReceiptInfoService.list(new QueryWrapper<WorkReceiptInfo>()
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				.eq("rel_biz_type", idType)
				.eq("rel_biz_id", id)
				);
		
		List<WorkReceiptModel> result=new ArrayList<WorkReceiptModel>();
		
		if(!CollectionUtils.isEmpty(list)) {
			Set<Integer> userIds=new HashSet<Integer>();
			list.forEach(item->{
				if(CheckUtils.hasInt(item.getUserId())) {
					userIds.add(item.getUserId());
				}
			});
			BaseClientResult<Map<Integer, UserSimple>> userMapResult=userRemoteService.getUserMapByIds(userIds);
			Map<Integer, UserSimple> userMap=BaseClientResult.getResultData(userMapResult);
			list.forEach(item->{
				result.add(CrmWorkChangeUtils.getWorkReceiptModel(item,userMap));
			});
		}
		
		return result;
	}


	@Override
	public Map<String, List<WorkReceiptModel>> listByRel(String idType, Set<String> id, LoginSimpleUserInfo userInfo) {
	
		List<WorkReceiptInfo> list= workReceiptInfoService.list(new QueryWrapper<WorkReceiptInfo>()
				.eq("rows_state", EnabledEnum.ENABLED.getCode())
				.eq("rel_biz_type", idType)
				.in("rel_biz_id", id)
				);
		
		Map<String, List<WorkReceiptModel>> result=new HashMap<String, List<WorkReceiptModel>>();
		
		if(!CollectionUtils.isEmpty(list)) {
			Set<Integer> userIds=new HashSet<Integer>();
			list.forEach(item->{
				if(CheckUtils.hasInt(item.getUserId())) {
					userIds.add(item.getUserId());
				}
			});
			BaseClientResult<Map<Integer, UserSimple>> userMapResult=userRemoteService.getUserMapByIds(userIds);
			Map<Integer, UserSimple> userMap=BaseClientResult.getResultData(userMapResult,new HashMap<>());
			list.forEach(item->{
				List<WorkReceiptModel> wrList=result.get(item.getRelBizId());
				if(wrList==null) {
					wrList=new ArrayList<WorkReceiptModel>();
					result.put(item.getRelBizId(), wrList);
				}
				wrList.add(CrmWorkChangeUtils.getWorkReceiptModel(item,userMap));
			});
		}
		
		return result;
	}
	
	
	
}
